package com.cqupt.scanning.system.admin.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cqupt.scanning.system.acl.entity.AclUser;
import com.cqupt.scanning.system.acl.service.AclUserService;
import com.cqupt.scanning.system.admin.entity.Department;
import com.cqupt.scanning.system.admin.entity.DeviceFoot;
import com.cqupt.scanning.system.admin.entity.Organization;
import com.cqupt.scanning.system.admin.entity.PatientFoot;
import com.cqupt.scanning.system.admin.entity.dto.PatientFootDTO;
import com.cqupt.scanning.system.admin.mapper.PatientFootMapper;
import com.cqupt.scanning.system.admin.service.DepartmentService;
import com.cqupt.scanning.system.admin.service.DeviceFootService;
import com.cqupt.scanning.system.admin.service.OrganizationService;
import com.cqupt.scanning.system.admin.service.PatientFootService;
import com.cqupt.scanning.system.file.helper.GenerateFootPDF;
import com.cqupt.scanning.system.statistics.entity.CalculatedDataFoot1;
import com.cqupt.scanning.system.utils.result.R;
//import org.omg.CORBA.PUBLIC_MEMBER;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 宋宝梁
 * @since 2021-07-06
 */
@Service
public class PatientFootServiceImpl extends ServiceImpl<PatientFootMapper, PatientFoot> implements PatientFootService {

    private AclUserService aclUserService;
    private DeviceFootService deviceFootService;
//    private PatientFootService patientFootService;

    @Autowired
    public PatientFootServiceImpl(AclUserService aclUserService,DeviceFootService deviceFootService){
        this.aclUserService = aclUserService;
        this.deviceFootService = deviceFootService;
//        this.patientFootService = patientFootService;
    }

    @Override
    public R getAll(Page<PatientFoot> page,String name, String ageLower,String ageUpper, String doctor, String heightLower, String heightUpper, String measureMode, String sex, String weightLower, String weightUpper, String beginTime, String endTime,String userName,String phone,String orgName,String devNum) {
        QueryWrapper<PatientFoot> w = new QueryWrapper<>();
        if (!StrUtil.isEmpty(name)){
            w.like("name",name);
        }
        if (!StrUtil.isEmpty(ageLower) && !StrUtil.isEmpty(ageUpper)){
            w.ge("age", ageLower).le("age", ageUpper);
        }
         if (!StrUtil.isEmpty(doctor)){
            w.like("doctor",doctor);
        }
       if (!StrUtil.isEmpty(heightLower) && !StrUtil.isEmpty(heightUpper)){
            w.ge("height",heightLower).le("height",heightUpper);
        }
         if (!StrUtil.isEmpty(measureMode)){
            w.like("gaitMeasureMode",measureMode);
        }
        if (!StrUtil.isEmpty(sex)){
            w.eq("sex", sex);
        }
        if (!StrUtil.isEmpty(weightLower) && !StrUtil.isEmpty(weightUpper)){
            w.ge("weight",weightLower).le("weight",weightUpper);
        }
        if (!StrUtil.isEmpty(orgName)){
            w.like("org_name",orgName);
        }
        if (!StrUtil.isEmpty(devNum)){
            w.like("dev_num",devNum);
        }
        if (!StrUtil.isEmpty(beginTime) && !StrUtil.isEmpty(endTime)){
            w.ge("create_time",beginTime).le("create_time",endTime);
        }
        if(!StrUtil.isEmpty(phone)){
            w.like("phone",phone);
        }
        if (!StrUtil.isEmpty(userName)){
            AclUser user = aclUserService.getOne(new QueryWrapper<AclUser>().eq("note",0).eq("username", userName).select("id"));
            DeviceFoot deviceFoot = deviceFootService.getOne(new QueryWrapper<DeviceFoot>().eq("user_id", user.getId()).select("dev_num"));
            w.like("dev_num",deviceFoot.getDevNum());
        }

        IPage<PatientFoot> patientFootIPage = baseMapper.selectPage(page, w);//根据 w(可为null)的条件，查询全部记录（并翻页）
        List<PatientFoot> records = patientFootIPage.getRecords();
        List<PatientFootDTO> patientFoots = new ArrayList<>();
        for (PatientFoot record : records) {
            PatientFootDTO patientFootDTO = new PatientFootDTO();
            BeanUtils.copyProperties(record, patientFootDTO);
            patientFoots.add(patientFootDTO);
        }
        System.out.println("第"+patientFootIPage.getCurrent()+"页，本次查询共有条数为"+patientFootIPage.getTotal());
        return R.ok().data("patients", patientFoots).data("total", patientFootIPage.getTotal());
    }



}
